//
//  IBDButton.m
//  LocalShop
//
//  Created by admin on 16/4/25.
//  Copyright © 2016年 cqsf. All rights reserved.
//

#import "IBDButton.h"

#define kWidth self.frame.size.width
#define kHeight self.frame.size.height


@implementation IBDButton
{
    UIButton *_bkButton;
    UILabel *_oneLbl;
    UILabel *_twoLbl;
    UILabel *textLbl ;
    UIButton *imgBtn;
    Action _action;

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setIgInster:(CGFloat)igInster
{
    _igInster = igInster;
    imgBtn.frame = CGRectMake(textLbl.endPointX + _igInster, 0, self.frame.size.height, self.frame.size.height);

}
-(void)setBkColor:(UIColor *)bkColor
{
    _bkColor = bkColor;
    self.backgroundColor = _bkColor;
    _bkButton.backgroundColor =_bkColor;
    textLbl.backgroundColor =_bkColor;
    imgBtn.backgroundColor =_bkColor;
}
-(void)setOneTitle:(NSString *)oneTitle
{
    _oneTitle = oneTitle;
    _oneLbl.text = oneTitle;
}
-(void)setTwoTitle:(NSString *)twoTitle
{
    _twoTitle = twoTitle;
    _twoLbl.text  = twoTitle;
}
-(void)setText:(NSString *)text
{
    textLbl.text = text;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat h = frame.size.height/3;
        CGFloat w = frame.size.width;
        CGFloat jh = h/2;
        
        _bkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_bkButton];
        _bkButton.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        
        _oneLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,jh,w,h)];
        [_bkButton addSubview:_oneLbl];
//        [_oneLbl addTextColor:UIColorFromRGB(0x323232) andTextFont:15];
        _oneLbl.textColor = UIColorHex(0x323232);
        _oneLbl.font = [UIFont systemFontOfSize:15];
 
        _twoLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,_oneLbl.endPointY+_oneLbl.frame.size.height,w,h)];
        [_bkButton addSubview:_twoLbl];
        _twoLbl.textColor = UIColorHex(0x646464);
        _twoLbl.font = [UIFont systemFontOfSize:12];
        
        _oneLbl.textAlignment = NSTextAlignmentCenter;
        _twoLbl.textAlignment = NSTextAlignmentCenter;
        
        
    }
    return self;
}

+ (instancetype)cutomBtnFream:(CGRect)frame  andText:(NSString *)text andTextColor :(UIColor *)color andImg:(UIImage *)img
{
    IBDButton *btn = [[self alloc] initWithFrame:frame BtnText:text andTextColor:color andImg:img];
    
    return btn;
}

-(instancetype)initWithFrame:(CGRect)frame BtnText:(NSString *)text andTextColor :(UIColor *)color andImg:(UIImage *)img
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _bkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _bkButton.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self addSubview:_bkButton];
        
        textLbl = [[UILabel alloc] init];
        
        imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bkButton addSubview:textLbl];
        [_bkButton addSubview:imgBtn];
        imgBtn.userInteractionEnabled = NO;
        
        _bkButton.backgroundColor = [UIColor whiteColor];
        textLbl.frame = CGRectMake(0, 0, frame.size.width/3 * 2, frame.size.height);
        
        [imgBtn setImage:img forState:UIControlStateNormal];
        [imgBtn setImage:img forState:UIControlStateHighlighted];
        [imgBtn setImage:img forState:UIControlStateSelected];
        
        textLbl.text = text;
        textLbl.textColor = color;
        textLbl.textAlignment = NSTextAlignmentRight;
        
        textLbl.numberOfLines = 0;
        [textLbl sizeToFit];
        
        textLbl.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        imgBtn.frame = CGRectMake(textLbl.endPointX + _igInster, 0, frame.size.height, frame.size.height);
        
    }
    return self;
}

-(void)addTarget:(id)targets  andAction:(SEL)action
{
    [_bkButton addTarget:targets action:action forControlEvents:UIControlEventTouchUpInside];
}






- (instancetype)initWithFrame:(CGRect)frame style:(IBDButtonStyle)style action:(Action)action {
    if (self = [super initWithFrame:frame]) {
        [self setupSubviewsWithStyle:style ];
        

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
        _action = action;
        
        self.alpha = 0.2;
    }
    return self;
}

- (void)tapAction {
    if (_action) {
        _action(self);
    }
}

- (void)setupSubviewsWithStyle:(IBDButtonStyle)style {
    switch (style) {
        case IBDButtonStyleDefault:
            
            break;
        case IBDButtonStyleTwoLabel:
        {
            self.headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.frame.size.width, (self.frame.size.height -20)* 0.6 )];
            self.headLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.headLabel];
            
            self.footLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headLabel.frame), self.frame.size.width, (self.frame.size.height  - 20) * 0.4)];
            self.footLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.footLabel];
        }
            break;
        case IBDButtonStyleLabelAndImage:
        {
            self.headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-10, self.frame.size.height)];
            self.headLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.headLabel];
            
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 10, (self.frame.size.height-10)/2.0, 10, 10)];
            [self addSubview:self.imageView];
        }
            break;
        case IBDButtonStyleImageAndLabel:
        {
            self.imageView = [[UIImageView alloc] init];
            [self addSubview:self.imageView];
            
            self.headLabel = [UILabel new];
            self.headLabel.textColor = UIColorHex(0x323232);
            self.headLabel.font = [UIFont systemFontOfSize:14.0f];
            [self addSubview:self.headLabel];
            
//            self.imageView.center = CGPointMake(self.frame.size.width/5,self.bounds.size.height);
            self.imageView.backgroundColor = [UIColor yellowColor];
            self.imageView.frame = CGRectMake(self.frame.size.width/4/2, self.frame.size.height/3/2, self.frame.size.height/4/2, self.frame.size.height/4/2);
            
        }
            break;
        case IBDButtonStyleImageAndLabel2:
        {
            self.imageView = [[UIImageView alloc] init];
            [self addSubview:self.imageView];
            
            self.headLabel = [UILabel new];
            self.headLabel.textColor = UIColorHex(0x323232);
            self.headLabel.font = [UIFont systemFontOfSize:14.0f];
            [self addSubview:self.headLabel];
            
            //            self.imageView.center = CGPointMake(self.frame.size.width/5,self.bounds.size.height);
            self.imageView.backgroundColor = [UIColor yellowColor];
            self.imageView.frame = CGRectMake(self.frame.size.width/4/2, self.frame.size.height/3/2, self.frame.size.height/4/2, self.frame.size.height/4/2);
            
        }
            break;

    }


}

- (void)setImageViewInsets:(UIEdgeInsets)imageViewInsets {
    _imageViewInsets = imageViewInsets;
    [self.imageView setFrame:CGRectMake(_imageViewInsets.left, (kHeight - 13)/2.0, 14, 13)];
    [self.headLabel setFrame:CGRectMake(CGRectGetMaxX(self.imageView.frame)+5, 0, kWidth - 14 - 5 - _imageViewInsets.left, kHeight)];
}

- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageView.image = image;
}

- (void)setHead:(NSString *)head {
    _head = head;
    self.headLabel.text = head;
}


@end
